package offer;

public class offer66 {
    public static int[] constructArr(int[] a) {
        if(a.length==0||a.length==1) return new int[1];
        int []res = new int[a.length];
//        构建前缀积和后缀积
        int []leftToRight = new int[a.length];
        int []rightToLeft = new int[a.length];
        leftToRight[0]=a[0];
        int ans =1;
        rightToLeft[a.length-1] = a[a.length-1];
        for (int i = 1; i <a.length ; i++) {
            leftToRight[i] = leftToRight[i-1]*a[i];
            rightToLeft[a.length-1-i] = rightToLeft[a.length-i]*a[a.length-i-1];
        }
        for (int i = 0; i <a.length ; i++) {
            if(i>0&&i<a.length-1)
            {
                ans = leftToRight[i-1]* rightToLeft[i+1];
            }
            if(i==0)
            {
                ans = rightToLeft[1];
            }else if(i==a.length-1)
            {
                ans = leftToRight[a.length-2];
            }
            res[i] = ans;
        }
        return res;
    }

    public static void main(String[] args) {
//        [1, 2, 3, 4, 5]
        int []num = {1, 2, 3, 4, 5};
        constructArr(num);
    }
}
